<%!String title = "Browse Pizzas";%>

<%@ include file="/header.jsp"%>

<%!int firstPizza = 0;%>
<%!int thisPage = 0;%>

<%
	if (request.getParameter("page") != null) {
		firstPizza = 10 * Integer
				.parseInt(request.getParameter("page"));
		thisPage = Integer.parseInt(request.getParameter("page"));

	}
	//System.out.println("This page: " + thisPage + ", First pizza: "
	//		+ firstPizza);
%>
<br><br>
<%
	ArrayList<dk.itu.pam.model.Pizza> contextPizzas = (ArrayList<Pizza>) c
			.getAttribute("pizzas");
%>
<%
	ArrayList<dk.itu.pam.model.Pizza> sessionPizzas = (ArrayList<Pizza>) s
			.getAttribute("pizzas");
%>
<%
	ArrayList<User> contextUsers = (ArrayList<User>) c
			.getAttribute("users");
%>
<%
	ArrayList<Admin> contextAdmins = (ArrayList<Admin>) c
			.getAttribute("admins");
%>
<%
	String minPrice = (String) s.getAttribute("min_price");
%>
<%
	String maxPrice = (String) s.getAttribute("max_price");
%>

<form method=post action=controller>
<b> Minimum price:</b> <input name="browse_minprice" type="text" size="10" <% if (minPrice != null) {out.print("value="+minPrice); } %>>
<b> Maximum price:</b> <input name="browse_maxprice" type="text" size="10" <% if (maxPrice != null) {out.print("value="+maxPrice); } %>>
<input type="submit" name="browse_filter" value="Filter by price range" />
</form>

<table>
<%
		if (sessionPizzas == null) {
			sessionPizzas.addAll(contextPizzas);
		}

		//GET PIZZAS FROM REMOTE WEB SERVICE
		//if (s.getValue("imp") == null) {
		//	sessionPizzas.addAll(Parser.getRemotePizzas("", ""));
		//	s.setAttribute("imp", "1");
		//}
		
		if (sessionPizzas != null) {
			//System.out.println("math min: "
			//		+ Math.min(firstPizza + 10, contextPizzas.size()
			//				- (10 * thisPage)));
			for (int i = firstPizza; i < Math.min(firstPizza + 10,
					firstPizza + (sessionPizzas.size() - (10 * thisPage))); i++) {
				Pizza p = sessionPizzas.get(i);
				//System.out.println("Printing pizzas.. i: " + i);
				out.print("<tr class=\"pizza_info\"><td>"
						+ p.toString()
						+ "</td> <td> <form method=\"post\" action=\"controller\">"
						+ "<input type=\"submit\" name=\"browse_add\" value=\"add to basket\">"
						+ "<input type=\"hidden\" name=\"browse_pizzaid\" value=\"");
				out.print(p.getId());
				out.print("\"></form></td></tr>");
			}
		}
	%>
	
</table>

<br>
<br>


<%
	if (thisPage > 0) {
		out.print("<a id=\"backward\" href=\"browse.jsp?page=");
		out.print("" + (thisPage - 1));
		out.print("\">previous page</a> || ");
	} else {
		out.print("<a id=\"backward\" />");
	}
	if (contextPizzas.size() > (thisPage + 1) * 10) {
		out.print("<a href=\"browse.jsp?page=");
		out.print("" + (thisPage + 1));
		out.print("\">next page</a>");
	}
%>
</body>
</html>